var touringcar_collection = db.collection("touringcar");
(function() {
    // 构造函数
    var _con = function(info, admin) {
        var self = this;
        self.num = 10;
        if (info) {
            /*printData("房车：", info, admin);
            self.type = "module";*/
            // 自有id
            self.info = info;
            // 创建者
            self.creater = admin || {};
            self.id = "car_" + Math.random().toString(16).substr(2, 2) + (new Date()).getTime().toString(16);
            // 创建时间
            self.createtime = (new Date()).getTime();
            // 模块名称
            /*self.name = info.name;
            // 创建者
            self.creater = admin || {};
            self.creater_id = admin._id;
            // 图标
            //self.preview_img = info.preview_img;
            // 创建时间
            self.createtime = (new Date()).getTime();*/
        };
    };

    // 导出类
    module.exports = _con;

    //查找房车
    _con.prototype.getlist = co.wrap(function*(){
        var self = this;
        if(!self.creater._id){
            return printError("您不是管理员！");
        }
        var page = parseInt(self.info.page);
        page = Math.max(1,page)-1;

        var param = {
            skip: self.num * page,
            limit: self.num,
            sort: {
                createtime: -1
            }
        };
        return yield mongo.find(touringcar_collection,param);
    })

    // 添加房车
    _con.prototype.save = co.wrap(function*() {
        var self = this;
        var data = self.info;
        if(!self.creater._id){
            return printError("您不是管理员！");
        }
        if(!data.title){
            return printError("标题不能为空！");
        }
        if(!data.image){
            return printError("图片不能为空！");
        }
        data.rentprice = parseFloat(data.rentprice);
        if(data.rentprice<=0){
            return printError("价格不能为空！");
        }
        if(!data.module){
            return printError("型号不能为空！");
        }
        var carmodul_collection = db.collection("carmodul");
        var obj_carmodul = yield mongo.findOne(carmodul_collection,{
            query:{
                _id:data.module
            }
        });
        if(!obj_carmodul){
            return printError("型号不存在！");
        }
        if(!data.numseats){
            return printError("座位不能为空！");
        }
        if(!data.everyyearlong){
            return printError("每年使用时长不能为空！");
        }
        if(!data.years){
            return printError("使用年限不能为空！");
        }
        data.module_name = obj_carmodul.name;
        data._id = self.id;
        data.createtime = self.createtime;
        //return data;
        return yield mongo.insertOne(touringcar_collection,data);
    });

    // 修改房车
    _con.prototype.update = co.wrap(function*() {
        var self = this;
        var _id = self.info._id;
        var recive_data = self.info;
        delete recive_data._id;
        if(recive_data.module){
            var carmodul_collection = db.collection("carmodul");
            var obj_carmodul = yield mongo.findOne(carmodul_collection,{
                query:{
                    _id:recive_data.module
                }
            });
            if(!obj_carmodul){
                return printError("型号不存在！");
            }
        }
        var datas = yield mongo.updateOne(touringcar_collection,{
            query:{
                _id:_id
            },
            data:recive_data
        });
        return datas;
    });

    //删除指定房车
    _con.prototype.delete = co.wrap(function*(_id){
        var self = this;
        if(!self.creater._id){
            return printError("您不是管理员！");
        }
        var _doc = mongo.removeOne(touringcar_collection,{
            query:{
                _id:_id
            }
        });
        return _doc;
    })
}());